package com.intelligence.tio.server.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.Aio;
import org.tio.core.ChannelContext;
import org.tio.json.Json;

import com.intelligence.tio.common.Command;
import com.intelligence.tio.common.JsonPacket;
import com.intelligence.tio.common.TioSessionContext;
import com.intelligence.tio.common.intf.AbsTioBsHandler;
import com.intelligence.tio.common.packages.P2PReqBody;
import com.intelligence.tio.common.packages.P2PRespBody;

/**
 * 
* @Title: P2PReqHandler.java 
* @Package com.intelligence.tio.server.handler 
* @Description: 点对点私聊消息处理器
* @Company: 南京因坦利软件有限公司
* @author 郭浩柱  
* @date 2017年5月18日 下午3:47:25 
* @version V1.0
 */
public class P2PReqHandler extends AbsTioBsHandler<P2PReqBody>
{
	private static Logger log = LoggerFactory.getLogger(P2PReqHandler.class);

	/** 
	 * @return
	 * @author: tanyaowu
	 */
	@Override
	public Class<P2PReqBody> bodyClass()
	{
		return P2PReqBody.class;
	}

	/** 
	 * @param packet
	 * @param bsBody
	 * @param channelContext
	 * @return
	 * @throws Exception
	 * @author: tanyaowu
	 */
	@Override
	public Object handler(JsonPacket packet, P2PReqBody bsBody, ChannelContext<TioSessionContext, JsonPacket, Object> channelContext) throws Exception
	{
		log.info("收到点对点请求消息:{}", Json.toJson(bsBody));
		
		TioSessionContext sessionContext = channelContext.getSessionContext();
		
		P2PRespBody p2pRespBody = new P2PRespBody();
		p2pRespBody.setToUserId(bsBody.getToUserId());
		p2pRespBody.setFromUserId(bsBody.getFromUserId());
		p2pRespBody.setFromUserName(bsBody.getFromUserName());
		p2pRespBody.setFromUserPhone(bsBody.getFromUserPhone());
		p2pRespBody.setCommand(Command.COMMAND_CHAT_RESP.getValue());
		p2pRespBody.setText(bsBody.getText());
		p2pRespBody.setFromRoleId(sessionContext.getRoleId());
		JsonPacket respPacket = new JsonPacket();
		respPacket.setBody(Json.toJson(p2pRespBody).getBytes(JsonPacket.CHARSET));
		Aio.sendToUser(channelContext.getGroupContext(), bsBody.getToUserId(), respPacket);
		return null;
	}
}
